/*
Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.
*/
package GuiTools;

import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.*;
import Settings.*;
import Storage.*;

/**
 * Panel containing either a ComboBox or a TextField for the article, one TF for the meaning and a ComboBox for the category.
 * @author Christoph Schnack
 *
 */
public class TermPanel extends JPanel{
	String article="",meaning="";
	TribuitTextField termTF,articleTF;
	LabelPanel mtfLP,catLP;
	JComboBox articleCB,catCB;
	TermClass tc;
	String index;
	Settings SET;
	Language LANG;
	LabelPanel articlePanel;
	
	/**
	 * Tralalalala
	 * 
	 * @param set Settings instance
	 * @param t term class
	 * @param cats category vector
	 * @param nr title
	 */
	public TermPanel(Settings set, TermClass t, Vector<String> cats, String title){
		super();
		LANG = set.getLanguage();
		SET = set;
		tc=t;
		index=title;
		termTF=new TribuitTextField(SET,10);
		articleTF=new TribuitTextField(SET,3);
		if(tc.hasPrefix()){
			if(tc.getPreFixVal()==null){
				articlePanel = new LabelPanel(tc.getPreFixName(),articleTF);
			}
			else{
				articleCB=new JComboBox(tc.getPreFixVal());
				articleCB.setPreferredSize(new Dimension(80,articleCB.getPreferredSize().height));
				articlePanel = new LabelPanel(tc.getPreFixName(),articleCB);
			}
			this.add(articlePanel);
		}
		if(cats==null || cats.size()==0){
			cats = new Vector<String>();
			cats.add(LANG.getNone());
		}
		catCB = new JComboBox(cats);
		catCB.setPreferredSize(new Dimension(80,catCB.getPreferredSize().height));
		mtfLP = new LabelPanel(LANG.getMeaning()+" "+index+":",termTF);
		catLP = new LabelPanel(LANG.getCategory()+":",catCB);
		this.add(mtfLP);
		this.add(catLP);
		this.setAllOpaque(false);
	}
	
	public void setUpTerm(Term t){
		setArticle(t.getArticle());
		setMeaning(t.getTerm());
		setCategory(t.getCategory());
	}
	
	public void grabFocusMeaningTF(){
		termTF.grabFocus();
	}
	
	public void grabFocusArticle(){
		if(tc.hasPrefix()){
			if(tc.getPreFixVal()==null) articleTF.grabFocus();
			else articleCB.grabFocus();
		}
	}
	
	public void grabFocusFirstComponent(){
		if(tc.hasPrefix()){
			if(tc.getPreFixVal()==null) articleTF.grabFocus();
			else{
				if(tc.getPreFixVal().size()<2) termTF.grabFocus();
				else articleCB.grabFocus();
			}
		}
		else termTF.grabFocus();
	}
	
	public void removeCategoryLP(){
		this.remove(catLP);
	}
	
	public LabelPanel getCategoryLP(){
		return catLP;
	}
	
	public void resetAllData(){
		catCB.setSelectedIndex(0);
		if(tc.hasPrefix()){
			if(tc.getPreFixVal()==null) articleTF.setText("");
			else articleCB.setSelectedIndex(0);
		}
		termTF.setText("");
	}
	/**
	 * Gets String from termTF and calls trim() on it before returning.
	 * @return
	 */
	public String getMeaning(){return termTF.getText().trim();}
	public void setMeaning(String s){
		if(s!=null){
			meaning = s;
			termTF.setText(s);
		}
	}
	public String getArticle(){
		return (tc.getPreFixVal()==null)? articleTF.getText():(String) articleCB.getSelectedItem();
	}
	public void setArticle(String s){
		if(tc.hasPrefix()){
			if(s!=null){
				if(tc.getPreFixVal()==null) articleTF.setText(s);
				else articleCB.setSelectedItem(s);
			}
		}
	}
	public String getCategory(){
		return (String) catCB.getSelectedItem();
	}
	
	public void setCategory(String s){
		catCB.setSelectedItem(s);
	}
	
	
	public TribuitTextField getMeaningTF(){
		return termTF;
	}
	
	public JComponent getArticleComponent(){
		if(tc.hasPrefix())return tc.getPreFixVal()==null?articleTF:articleCB;
		else return null;
	}
	
	public JComponent getCategoryComponent(){
		return catCB;
	}
	
	public void setTFActionListener(ActionListener al){
		termTF.addActionListener(al);
	}
	
	public void putFocusOnMeaningTFAndSelectAll(){
		termTF.requestFocus();
		termTF.selectAll();
	}
	
	public void setTermClass(TermClass t){
		this.removeAll();
		tc = t;
		if(t.hasPrefix()){
			if(t.hasPrefix()){
				if(t.getPreFixVal()!=null){
					articleCB = new JComboBox(t.getPreFixVal());
					articlePanel = new LabelPanel(t.getPreFixName(),articleCB);
				}
				else{
					articleTF = new TribuitTextField(SET,3);
					articlePanel = new LabelPanel(t.getPreFixName(),articleTF);
				}
				this.add(articlePanel);
			}
		}
		mtfLP = new LabelPanel(LANG.getMeaning()+" "+index+":",termTF);
		catLP = new LabelPanel(LANG.getCategory()+":",catCB);
		this.add(mtfLP);
		this.add(catLP);
		setAllOpaque(false);
	}
	
	public void mark(){
		termTF.selectAll();
		termTF.requestFocus();
	}
	
	public void setAllOpaque(boolean arg){
		this.setOpaque(arg);
		mtfLP.setAllOpaque(arg);
		catLP.setAllOpaque(arg);
		if(articlePanel!=null)articlePanel.setOpaque(arg);
	}
	
	public String getLabelTitle(){
		return index;
	}
	
	public void setMeaningLPTitle(String s){
		index = s;
		mtfLP.setLabelText(LANG.getMeaning()+" "+s);
	}
}
